package com.homedo.microservice.open.service.gateway.manager.web.gateway.service;

import com.google.common.collect.Lists;
import com.homedo.common.bean.data.enums.ResponseEnum;
import com.homedo.common.bean.vo.context.UserContext;
import com.homedo.common.dao.bean.po.base.BasePo;
import com.homedo.common.exception.HomedoException;
import com.homedo.common.service.BaseService;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.AlertUserRouteService;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.AlertUserEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.AlertUserRouteServiceEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.RouteServiceEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.persistence.dao.AlertUserRouteServiceDao;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.support.ApplicationNameHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 作者 jgshun
 * @date 创建时间 2018年10月16日 16时44分30秒
 */
@Service
public class AlertUserRouteServiceService extends BaseService<AlertUserRouteService> {
    @Autowired
    private AlertUserRouteServiceDao alertUserRouteServiceDao;
    @Autowired
    private AlertUserService alertUserService;
    @Autowired
    private RouteServiceService routeServiceService;


    @Override
    public List<AlertUserRouteServiceEnhanced> getEnhanceds(List<?> ids, UserContext userContext) {
        List<AlertUserRouteService> alertUserRouteServices = alertUserRouteServiceDao.getListByIds(ids, AlertUserRouteService.class);

        if (!Detect.notEmpty(alertUserRouteServices)) {
            return null;
        }

        return this.convent2Enhanceds(alertUserRouteServices);
    }


    @Override
    public List<AlertUserRouteServiceEnhanced> getEnhanceds4Active(List<?> ids, UserContext userContext) {
        List<AlertUserRouteService> alertUserRouteServices = alertUserRouteServiceDao.getListByIdsGreaterThanMark0(ids, AlertUserRouteService.class);

        if (!Detect.notEmpty(alertUserRouteServices)) {
            return null;
        }

        return this.convent2Enhanceds(alertUserRouteServices);
    }


    @Override
    public List<AlertUserRouteServiceEnhanced> convent2Enhanceds(List<? extends BasePo> pos) {
        List<AlertUserRouteService> alertUserRouteServices = (List<AlertUserRouteService>) pos;
        List<AlertUserRouteServiceEnhanced> alertUserRouteServiceEnhanceds = Lists.newArrayList();
        alertUserRouteServices.forEach(e -> alertUserRouteServiceEnhanceds.add(new AlertUserRouteServiceEnhanced(e)));
        return alertUserRouteServiceEnhanceds;
    }


    public List<AlertUserRouteServiceEnhanced> getEnhancedsByServiceIds(List<String> serviceIds, UserContext userContext) {
        List<AlertUserRouteService> alertUserRouteServices = alertUserRouteServiceDao.getListByKeyValues(AlertUserRouteService.FIELD_SERVICEID, serviceIds, AlertUserRouteService.class);

        if (!Detect.notEmpty(alertUserRouteServices)) {
            return null;
        }

        return this.convent2Enhanceds(alertUserRouteServices);
    }

    public List<AlertUserRouteServiceEnhanced> appendAlertUserEnhanced(List<AlertUserRouteServiceEnhanced> alertUserRouteServiceEnhanceds, UserContext userContext) {
        List<Integer> alertUserIds = alertUserRouteServiceEnhanceds.stream().map(AlertUserRouteServiceEnhanced::getAlertUserEnhanced).collect(Collectors.toList())
                .stream().map(AlertUserEnhanced::getId).collect(Collectors.toList());
        List<AlertUserEnhanced> alertUserEnhanceds = alertUserService.getEnhanceds(alertUserIds, userContext);
        if (!Detect.notEmpty(alertUserEnhanceds)) {
            return alertUserRouteServiceEnhanceds;
        }

        this.appendAlertUserEnhanced(alertUserRouteServiceEnhanceds, alertUserEnhanceds, userContext);

        return alertUserRouteServiceEnhanceds;
    }

    private void appendAlertUserEnhanced(List<AlertUserRouteServiceEnhanced> alertUserRouteServiceEnhanceds, List<AlertUserEnhanced> alertUserEnhanceds, UserContext userContext) {
        alertUserRouteServiceEnhanceds.forEach(e -> {
            this.appendAlertUserEnhanced(e, alertUserEnhanceds, userContext);
        });
    }

    private void appendAlertUserEnhanced(AlertUserRouteServiceEnhanced alertUserRouteServiceEnhanced, List<AlertUserEnhanced> alertUserEnhanceds, UserContext userContext) {
        alertUserEnhanceds.forEach(e -> {
            if (e.getId().intValue() == alertUserRouteServiceEnhanced.getAlertUserEnhanced().getId()) {
                alertUserRouteServiceEnhanced.setAlertUserEnhanced(e);
                return;
            }
        });
    }

    public void saveAlertUserRouteService(String serviceId, String username, UserContext userContext) {
        this.checkSave(serviceId, username);

        AlertUserEnhanced alertUserEnhanced = alertUserService.getEnhancedByUsername4Application(username, ApplicationNameHolder.get(), null);

        AlertUserRouteService alertUserRouteService = new AlertUserRouteService();
        alertUserRouteService.setApplicationName(ApplicationNameHolder.get());
        alertUserRouteService.setServiceId(serviceId);
        alertUserRouteService.setAlterUserId(alertUserEnhanced.getId());
        alertUserRouteService.setCreateTime(new Date());
        alertUserRouteService.setUpdateTime(new Date());

        alertUserRouteServiceDao.saveSelective(alertUserRouteService);
    }

    private void checkSave(String serviceId, String username) {
        RouteServiceEnhanced routeServiceEnhanced = routeServiceService.getEnhanced(serviceId, null);
        AlertUserEnhanced alertUserEnhanced = alertUserService.getEnhancedByUsername4Application(username, ApplicationNameHolder.get(), null);
        if (routeServiceEnhanced == null || alertUserEnhanced == null) {
            throw new HomedoException(ResponseEnum.PARAM_ERROR);
        }
        AlertUserRouteService alertUserRouteService = new AlertUserRouteService();
        alertUserRouteService.setAlterUserId(alertUserEnhanced.getId());
        alertUserRouteService.setServiceId(routeServiceEnhanced.getId());
        alertUserRouteService.setApplicationName(ApplicationNameHolder.get());

        alertUserRouteService = alertUserRouteServiceDao.getOneByRecord(alertUserRouteService);
        if (alertUserRouteService != null) {
            throw new HomedoException("[已配置]");
        }
    }

    public void deleteAlertUserIdAndServiceId(Integer alertUserId, String serviceId, UserContext userContext) {
        this.checkDelete(alertUserId, serviceId);
        AlertUserRouteService alertUserRouteService = new AlertUserRouteService();
        alertUserRouteService.setAlterUserId(alertUserId);
        alertUserRouteService.setServiceId(serviceId);
        alertUserRouteService.setApplicationName(ApplicationNameHolder.get());

        alertUserRouteServiceDao.delete(alertUserRouteService);
    }

    private void checkDelete(Integer alertUserId, String serviceId) {
        if (alertUserId == null || !Detect.notEmpty(serviceId)) {
            throw new HomedoException(ResponseEnum.PARAM_ERROR);
        }
    }
}
